package com.powerManager.sdk.handlers;

import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.powerManager.sdk.common.JdbcSingleton;
import com.powerManager.sdk.common.PagingResult;
import com.powerManager.sdk.common.Security;
import com.powerManager.sdk.entity.User;

import org.springframework.jdbc.core.JdbcTemplate;

public class GeneralHandler {
	
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//	UserLoginDao userDao = new UserLoginDao();
		//System.out.println(id+":"+name+":"+p1);
		System.out.println(request.getParameter("username"));
		//用户名
		String username = request.getParameter("username");
		//用户密码
		String password = request.getParameter("password");
		//用户登录
		String sql="select * from Sys_user ";

			sql=sql+" where login_name=? and password=?";

		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		List<Map<String,Object>> results = null;
		try {
			results = jdbc.queryForList(sql,new Object[]{username,password});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(results != null && results.size()>0){
			Map<String,Object> result = results.get(0);
			User u = new User();
			u.setUser_id((String)result.get("user_id"));
			u.setLoginname((String)result.get("login_name"));
			u.setNickname((String)result.get("nickname"));
			Security s = new Security();
			s.setCurrentUser(u);
			sql="select * from sys_department where department_id=?";
			Map<String, Object> dept = jdbc.queryForMap(sql, result.get("department_id"));
			s.setCurrentDept(dept);
			sql="select r.* from sys_role r INNER JOIN sys_role_user ru on ru.role_id=r.role_id where ru.user_id=?";
			List<Map<String, Object>> roles = jdbc.queryForList(sql, u.getUser_id());
			s.setCurrentRole(roles);
			request.getSession().setAttribute("securityUser", s);
			return "text:true";
		}else{
			return "text:false";
		}
	}
	
	
	
	
	
}
